<template>
    <div>
        <h3>发表评论</h3>
        <hr>
        <textarea maxlength="120" placeholder="请输入评论内容不超过120个字" v-model="msg" ></textarea>
        <mt-button type="primary" size="large" @click="postComment">发表评论</mt-button>
        <div class="cmt-list">
            <div class="cmt-item" v-for="(item, index) in comments" :key="index">
                <div class="cmt-title">
                    第{{index+1}}楼:&nbsp;&nbsp;用户:{{item.user}}&nbsp;&nbsp;发表时间{{item.add_time|timeFormat}}:
                </div>
                <div class="cmt-content">{{item.content}}</div>
            </div>
             
        </div>
        <mt-button type="danger" size="large" plain @click="getMore">加载更多</mt-button>
    </div>
</template>
<script>
// import {Toast} from 'mini-ui'
// import MintUI from 'mint-ui'
export default {
    data() {
        return {
            pageIndex:1,
            comments:[],
            msg:''
        }
    },
    props:["id"],
    methods: {
        getComment(){
            this.$http.get('api/getcomments/'+this.id+'?pageindex='+this.pageIndex).then(function(res){
                if(res.body.status==0){
                    this.comments=this.comments.concat(res.body.message)
                }
            })
        },
        getMore(){
            this.pageIndex++
            this.getComment(this.id,this.pageIndex)
        },
        postComment(){
            if(this.msg.trim().length==0){
                return 
            }
            // 给后台服务器发内容
            this.$http.post('api/postcomment/'+this.id,{content:this.msg}).then(function(res){
                if(res.body.status==0){
                   
                    this.comments.unshift({user:"匿名用户",add_time:Date.now(),content:this.msg})
                    this.msg=''
                }
            })
        }

    },
    created() {
        this.getComment()
    },
}
</script>   
<style lang="scss" scoped>
textarea{height: 100px;font-size: 12px;}
.cmt-list{
    margin-top: 20px;
    .cmt-item{
        margin-bottom: 20px;
        .cmt-title{
            background: #ccc;
            line-height: 30px;
            font-size: 14px;
        }
        .cmt-content{
            font-size: 12px;
            text-indent: 2em;
            padding-top: 10px;
        }
    }

}
.mint-button--danger.is-plain{
    margin-bottom: 10px;
}
</style>
